﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Tomahawk.Runtime.Logic.UI;
using Microsoft.Xna.Framework;
using Tomahawk.Runtime.Localization;

namespace Tomahawk.Runtime.LoadingScreens
{
    public sealed class UIText : UIControl
    {
        #region Serializable Properties
        [XmlElement("ShadowEnabled")]
        public bool ShadowEnabled = false;

        [XmlElement("ShadowDisplacement")]
        public Point ShadowDisplacement = new Point(1,1);

        [XmlElement("ShadowColor")]
        public Color ShadowColor = Color.Black;

        [XmlAttribute("Alignment")]
        public XStyleManager.TextHAlign Alignment = XStyleManager.TextHAlign.Left;

        private string text = "";
        [XmlElement("Text")]
        public string Text 
        { 
            get { return text; } 
            set 
            { 
                text = value;
                localizedText = value;
                if (font != null)
                {
                    measuredSize = font.MeasureString(text);
                }
            } 
        }

        [XmlElement("Font")]
        public string FontName = "";

        [XmlElement("FontScale")]
        public float FontScale = 1.0f;

        [XmlElement("FontColor")]
        public Color FontColor = Color.White;
        #endregion

        #region Properties
        private string localizedText = "";
        private SpriteFont font = null;
        private Vector2 measuredSize;
        private Vector2 renderPosition;
        private Vector2 screenPosition;
        #endregion

        #region Loading
        internal override void Load(ContentManager contentManager, LocalizationManager localizationManager)
        {
            font = contentManager.Load<SpriteFont>(FontName);

            if (Localized)
            {
                localizationManager.GetLocalizedString(text, out localizedText);
            }

            base.Load(contentManager, localizationManager);
        }
        #endregion

        #region Localization
        internal override void Localize(ContentManager contentManager, LocalizationManager localizationManager)
        {
            if (Localized)
            {
                localizationManager.GetLocalizedString(text, out localizedText);
            }
            base.Localize(contentManager, localizationManager);
        }
        #endregion

        #region Render
        internal override void Render(UICanvas canvas, SpriteBatch spriteBatch)
        {
            if (localizedText != null)
            {
                float screenScale;

                this.measuredSize = font.MeasureString(text);

                //render shadow
                if (ShadowEnabled)
                {
                    if (Alignment == XStyleManager.TextHAlign.Left)
                    {
                        //left alignment
                        renderPosition.X = RenderRectangle.X + ShadowDisplacement.X;
                        renderPosition.Y = RenderRectangle.Y + ShadowDisplacement.Y;
                    }
                    else if (Alignment == XStyleManager.TextHAlign.Centered)
                    {
                        //horizontally centered location
                        renderPosition.X = RenderRectangle.X + ShadowDisplacement.X + (RenderRectangle.Width / 2) - measuredSize.X / 2;
                        renderPosition.Y = RenderRectangle.Y + ShadowDisplacement.Y;
                    }
                    else
                    {
                        //right aligment
                        renderPosition.X = RenderRectangle.X + ShadowDisplacement.X + RenderRectangle.Width - measuredSize.X;
                        renderPosition.Y = RenderRectangle.Y + ShadowDisplacement.Y;
                    }

                    Color textShadowColor = Color.White;
                    textShadowColor.R = (byte)((canvas.overlayColor.R * ShadowColor.R) / 255.0f);
                    textShadowColor.G = (byte)((canvas.overlayColor.G * ShadowColor.G) / 255.0f);
                    textShadowColor.B = (byte)((canvas.overlayColor.B * ShadowColor.B) / 255.0f);
                    textShadowColor.A = (byte)((canvas.overlayColor.A * ShadowColor.A) / 255.0f);

                    //draw
                    canvas.ApplyVirtualCanvas(ref renderPosition, ref screenPosition);
                    canvas.ApplyVirtualCanvas(FontScale, out screenScale);
                    spriteBatch.DrawString(font, localizedText, screenPosition, textShadowColor, RenderRotation, RenderOrigin, screenScale, SpriteEffects.None, Depth + 0.01f);
                }

                if (Alignment == XStyleManager.TextHAlign.Left)
                {
                    //discard width and height from destinationRectangle
                    renderPosition.X = RenderRectangle.X;
                    renderPosition.Y = RenderRectangle.Y;
                }
                else if (Alignment == XStyleManager.TextHAlign.Centered)
                {
                    //hotizontally centered location
                    renderPosition.X = RenderRectangle.X + (RenderRectangle.Width / 2) - measuredSize.X / 2;
                    renderPosition.Y = RenderRectangle.Y;
                }
                else
                {
                    //right aligment
                    renderPosition.X = RenderRectangle.X + RenderRectangle.Width - measuredSize.X;
                    renderPosition.Y = RenderRectangle.Y;
                }

                Color textColor = Color.White;
                textColor.R = (byte)((canvas.overlayColor.R * FontColor.R) / 255.0f);
                textColor.G = (byte)((canvas.overlayColor.G * FontColor.G) / 255.0f);
                textColor.B = (byte)((canvas.overlayColor.B * FontColor.B) / 255.0f);
                textColor.A = (byte)((canvas.overlayColor.A * FontColor.A) / 255.0f);

                //render color
                canvas.ApplyVirtualCanvas(ref renderPosition, ref screenPosition);
                canvas.ApplyVirtualCanvas(FontScale, out screenScale);
                spriteBatch.DrawString(font, localizedText, screenPosition, textColor, RenderRotation, RenderOrigin, screenScale, SpriteEffects.None, this.Depth);
            }
        }
        #endregion
    }
}
